<template>
  <div class="app-container">
    <toolbar
      @changePeriod="handleChangePeriod"
      @refresh="handleRefresh"
    ></toolbar>
    <div style="text-align: center; padding-top: 10px; padding-bottom: 10px">
      <div style="font-size: 16px; font-weight: bold">科目余额表</div>
      <div>{{ filter.startPeriod + " 至 " + filter.endPeriod }}</div>
    </div>
    <div>
      <el-table
        :data="datas"
        style="width: 100%; margin-bottom: 20px"
        row-key="accountId"
        border
        stripe
        fit
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="accountCode" label="编码" width="180">
        </el-table-column>
        <el-table-column prop="accountName" label="名称" width="180">
        </el-table-column>
        <el-table-column label="期初余额">
          <el-table-column label="借方">
            <template slot-scope="scope">
              {{
                scope.row.direction == 1
                  ? (scope.row.openingAmount | money)
                  : null
              }}
            </template></el-table-column
          >
          <el-table-column label="贷方">
            <template slot-scope="scope">
              {{
                scope.row.direction == -1
                  ? (scope.row.openingAmount | money)
                  : null
              }}
            </template></el-table-column
          >
        </el-table-column>
        <el-table-column label="本期发生额">
          <el-table-column label="借方">
            <template slot-scope="scope">
              {{ scope.row.debitAmount | money }}
            </template>
          </el-table-column>
          <el-table-column label="贷方"
            ><template slot-scope="scope">
              {{ scope.row.creditAmount | money }}
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="本期累计发生额">
          <el-table-column label="借方"
            ><template slot-scope="scope">
              {{ scope.row.currentYearDebitAmount | money }}
            </template>
          </el-table-column>
          <el-table-column label="贷方"
            ><template slot-scope="scope">
              {{ scope.row.currentYearCreditAmount | money }}
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column label="期末">
          <el-table-column label="借方">
            <template slot-scope="scope">
              {{
                scope.row.direction == 1
                  ? (scope.row.closingAmount | money)
                  : null
              }}
            </template></el-table-column
          >
          <el-table-column label="贷方">
            <template slot-scope="scope">
              {{
                scope.row.direction == -1
                  ? (scope.row.closingAmount | money)
                  : null
              }}
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import { accountBalanceData } from "@/api/report";
import toolbar from "@/components/Bar/reportToolbar";
export default {
  components: {
    toolbar,
  },
  data() {
    return {
      datas: [],
      filter: {
        startPeriod: null,
        endPeriod: null,
      },
    };
  },
  methods: {
    getData() {
      this.datas = [];
      accountBalanceData(this.filter).then((res) => {
        if (!res) {
          this.datas = [];
          return;
        }

        this.datas = res.toTree();
      });
    },
    handleChangePeriod(values) {
      if (values && values.length == 2) {
        this.filter.startPeriod = values[0].toPeriod();
        this.filter.endPeriod = values[1].toPeriod();
      }
    },
    handleRefresh() {
      this.getData();
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.getData();
    });
  },
  watch: {
    periods: {
      handler(values) {
        if (values && values.length == 2) {
          this.filter.startPeriod = values[0].toPeriod();
          this.filter.endPeriod = values[1].toPeriod();
        }
      },
    },
  },
};
</script>
<style scoped></style>
